var express = require('express');
var router = express.Router();

const SourceModel = require('../db/SourceModel');
const Fun = require("../middlewares/Fun")
const moment = require('moment');
const now = () => {
    return moment().format('YYYY-MM-DD HH:mm:ss')
}
/***
 *  list 来源列表
 *  one 查找来源
 *  save 创建来源
 *  update 更新来源信息
 *  delete 删除来源信息
 */

router.post('/hadnamed', function (req, res, next) {
    if (!req.body.name) {
        return res.json({ code: 201, mes: '缺少必填信息！' });
    }

    SourceModel.findOne({ name: req.body.name }).then((data) => {
        if (data) {
            return res.json({ code: 201, mes: '该名称已经存在！' });
        } else {
            return res.json({ code: 200, mes: '该名称可用！' });
        }
    }).catch(() => {
        return res.json({ code: 201, mes: '发生意外错误！' })
    });

});

// 保存来源

router.post('/save', function (req, res, next) {
    // 获取条件
    const source = new SourceModel(req.body);
    let user = req.user


    SourceModel.find({ name: req.body.name }).then((data) => {
        if (data.length != 0) { return res.json({ code: 201, mes: '来源名称已经存在' }); }
        else {
            source.save().then(() => {
                Fun.saveLogs(user, req.ip.replace(/::ffff:/, ''), "创建患者来源成功");
                return res.json({ code: 200, mes: '来源创建成功' })
            }).catch(() => {
                Fun.saveLogs(user, req.ip.replace(/::ffff:/, ''), "创建患者来源失败");
                return res.json({ code: 201, mes: '发生意外错误！' })
            })
        }
    }).catch((err) => {
        console.log(err);
        return res.json({ code: 201, mes: '发生意外错误！' })
    });
});
// 更新来源信息
router.post('/update', function (req, res, next) {
    // 更改密码的判断需要额外处理
    const set = req.body;
    const id = req.body.id;
    let user = req.user;
    SourceModel.find({ name: req.body.name }).then((data) => {
        if (data.length != 0) { return res.json({ code: 201, mes: '来源名称已经存在' }); }
        else {
            return SourceModel.findById(id).then(data => {
                if (data == null) {
                    return res.json({ code: 201, mes: '传入id错误' });
                }
                else {
                    return SourceModel.update({ _id: id }, { $set: set }).then(
                        () => {
                            Fun.saveLogs(user, req.ip.replace(/::ffff:/, ''), "更行患者来源成功");
                            return res.json({ code: 200, mes: '更改成功' });
                        }
                    )
                }
            })
        }
    }).catch(err => {
        Fun.saveLogs(user, req.ip.replace(/::ffff:/, ''), "更行患者来源失败");
        return res.json({ code: 201, mes: '发生意外错误！', err });
    });
});
// 查找所有来源update 
router.get('/list', function (req, res, next) {
    SourceModel.find().then((sources) => {
        return res.json({ code: 200, sources });
    }).catch(() => {
        return res.json({ code: 201, mes: '发生意外错误！' });
    });
});
// 查找指定来源
router.get('/one/:id', function (req, res, next) {
    const id = req.params.id;
    SourceModel.findById(id).then((source) => {
        return res.json({ code: 200, source });
    }).catch(() => {
        return res.json({ code: 201, mes: '发生意外错误！' });
    });
});
// 删除接口来源
router.post('/delete', function (req, res, next) {
    const id = req.body.id;
    let user = req.user;
    SourceModel.remove({ _id: id }).then(() => {
        Fun.saveLogs(user, req.ip.replace(/::ffff:/, ''), "删除患者来源成功");
        return res.json({ code: 200, mes: '删除成功！' });
    }).catch(() => {
        Fun.saveLogs(user, req.ip.replace(/::ffff:/, ''), "删除患者来源失败");
        return res.json({ code: 201, mes: '发生意外错误！' });
    });
});
// 列表分页接口
module.exports = router;
